package com.jinghua.treatment.tools;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import org.apache.log4j.Logger;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author kriss
 * @ClassName:
 * @Description:
 * @date 2017/10/8 0008. 9:46
 */
public class QiNiuUtil {
    public static final String ACCESS_KEY = PropertyUtil.getConfig("qiniu.accessKey");
    public static final String SECRET_KEY =  PropertyUtil.getConfig("qiniu.secretKey");

    Logger logger = Logger.getLogger(QiNiuUtil.class);
    String upToken;
    UploadManager uploadManager;
    BucketManager bucketManager;

    public QiNiuUtil(String bucket) {
        Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
        upToken = auth.uploadToken(bucket, null, 3600 * 5, null);
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        uploadManager = new UploadManager(cfg);
        bucketManager = new BucketManager(auth, cfg);
    }

    public static void main(String args[]) {
        QiNiuUtil qiNiuUtil = new QiNiuUtil("");
        //20171013-10-56867322-B
      /*  String phonePrefix = "10-56867322-B";
        List<String> stuPhones = new ArrayList<>();
        stuPhones.add("13858732976");
        String fileName = qiNiuUtil.findFileByPrefix(DateUtil.getDateString(new Date(), "yyyyMMdd") + "-" + phonePrefix, stuPhones);
        System.out.println(fileName);*/

    }



    public String upload2QiNiu(byte[] file,String name) {
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = DateUtil.getDateString(new Date(), "yyyyMMdd")+UUID.randomUUID()+".jpg";
        String link = "";
        try {
            Response response = uploadManager.put(file, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            link = key;
            logger.info("上传文件成功:");
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                logger.error("上传文件出错:" + ex2.getMessage());
            }
        }
        return link;
    }

    public void deleteFromQiNiu(String key,String bucket) {
        try {
            Response response = bucketManager.delete(bucket, key);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            logger.info("删除文件成功:");
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                logger.error("删除文件出错:" + ex2.getMessage());
            }
        }
    }




}
